package com.tpp.user.controller;

import com.tpp.comm.util.LoginException;
import com.tpp.comm.vo.Result;
import com.tpp.user.service.LinkUserService;
import com.tpp.user.service.UserService;
import com.tpp.user.vo.LinkUserVo;
import com.tpp.user.vo.UserVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private LinkUserService linkUserService;

    @GetMapping("/p/login")
    public Result login(String phone,String password){
        UserVo login = null;
        try {
            login = userService.login(phone, password);
        } catch (LoginException e) {
            return Result.fail(e.getMessage());
        }

        return Result.success(login);
    }

    @GetMapping("/v/refreshToken")
    public Result refreshToken(@RequestHeader("token")String token){
        UserVo.TokenVo tokenVo1 = userService.refreshToken(token);
        return Result.success(tokenVo1);
    }

    @GetMapping("/v/findByToken")
    public Result findByToken(@RequestHeader("token")String token){
        List<LinkUserVo> byToken = linkUserService.findByToken(token);
        return Result.success(byToken);
    }


}
